package com.application.DataProvider;

import java.util.ArrayList;

import com.application.DTO.ChapterDTO;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ChapterDAO{

	Context _mcontext = null;

	public ChapterDAO(Context context){
		this._mcontext = context;
	}

	public boolean isExist(int id)
	{
		Log.d("PEERDROID","vao isExist chapter: ");
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		boolean result = false;
		Cursor cur = null;
		try 
		{
			String sql = "Select count(*) from Chapter where ID=" +  id;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				int i = Integer.valueOf(cur.getString(0));
				if(i != 0)
					result = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		Log.d("PEERDROID","thoat isExist chap: " + result);
		return result;
	}
	public String getName(int id)
	{
		String name = null;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select name from chapter where ID = " + id;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				name = cur.getString(0);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		return name;

	}

	public ChapterDTO getChapter(int ID)
	{
		ChapterDTO cDto = new ChapterDTO();
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select * from Chapter where id = " + ID;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				cDto.set_ID(Integer.valueOf(cur.getString(0)));
				cDto.set_Name(cur.getString(1));
				cDto.set_Note(cur.getString(2));
				cDto.set_IDSubject(Integer.valueOf(cur.getString(3)));
				cDto.set_ContentAbstract(cur.getString(4));
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return cDto;
	}
	public ArrayList<ChapterDTO> getChapterList(int IDSubject)
	{
		Log.d("PEERDROID", "vao get chapter list chapDAO: idsubject: " + IDSubject);
		ArrayList<ChapterDTO> chapList = new ArrayList<ChapterDTO>();

		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select * from chapter where IDSubject = " + IDSubject;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				ChapterDTO cDto = new ChapterDTO();
				cDto.set_ID(Integer.valueOf(cur.getString(0)));
				cDto.set_Name(cur.getString(1));
				cDto.set_Note(cur.getString(2));
				cDto.set_IDSubject(Integer.valueOf(cur.getString(3)));
				cDto.set_ContentAbstract(cur.getString(4));
				chapList.add(cDto);
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}

		return chapList;
	}
	public boolean getStatus(int idChap)
	{
		boolean result = false;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		try 
		{
			String sql = "Select Status from Chapter where ID = " + idChap;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				if(cur.getString(0).equals("1"))
					result = true;

			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		Log.d("PEERDROID","Result status : " + result);
		return result;
	}
	//Insert du lieu vao bang Chapter
	public int insertChapter(ChapterDTO ct_dto) {
		Log.d("PEERDROID","insert chap");
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			values.put("ID", ct_dto.get_ID());
			values.put("Name", ct_dto.get_Name());
			values.put("Note", ct_dto.get_Note());
			values.put("IDSubject", ct_dto.get_IDSubject());
			values.put("ContentAbstract", ct_dto.get_ContentAbstract());
			values.put("Status",ct_dto.isNew());

			result = provider.execInsertCommand(db, "Chapter", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}

	//Cap nhat bang Chapter
	public int updateChapter(ChapterDTO ct_dto)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{
			ContentValues values = new ContentValues();
			values.put("Name", ct_dto.get_Name());
			values.put("Note", ct_dto.get_Note());
			values.put("IDSubject", ct_dto.get_IDSubject());
			values.put("ContentAbstract", ct_dto.get_ContentAbstract());
			values.put("Status",ct_dto.isNew());

			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(ct_dto.get_ID())};
			result= provider.execUpdateCommand(db, "Chapter", values, whereClause, whereArgs);

		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}

		return result;
	}

	public int updateStatusChapter(boolean status, int idChap)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{
			ContentValues values = new ContentValues();
			values.put("Status",status);

			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(idChap)};
			result= provider.execUpdateCommand(db, "Chapter", values, whereClause, whereArgs);

		} catch (Exception e) {
			// TODO: handle exception
		}
		finally {
			db.close();
		}
		return result;
	}
	//Delete doi tuong ChapterDTO
	public int deleteChapter(int idChapter)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{	
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(idChapter)};	
			result=provider.execDeleteCommand(db, "Chapter", whereClause, whereArgs);
		} catch (Exception e) {
			// TODO: handle exception
		}
		finally {
			db.close();
		}
		return result;
	}
}
